'''
# -*- coding:utf-8 -*-
# @Project: DevOps_api
# @Date: 2023/3/16 - 23:27
# @Author: ysg
# @Describe: 

Ops_influxdb.py
'''
# from influxdb import InfluxDBClient
from libs.connect_influxdb.devops_influxdb import do_influx
from utils.Ops_logger import logger


def sava_Opsdb(dit):
    client = do_influx()
    # client = InfluxDBClient(host='localhost', port=8086)
    # client.switch_database('devops_info')
    # 定义待插入的数据
    if dit:
        json_body = [
            {
                "measurement": "test2_dev_ops",
                "tags": {
                    "host_ip": dit.get("host_ip"),
                    "host_name": dit.get("host_name"),
                    "CPU": dit.get("cpu"),
                    "RAM": dit.get("memory"),
                    "DISK": dit.get("disk"),
                    "net_recvMB": dit.get("recvMB"),
                    "net_sentMB": dit.get("sentMB"),
                },
                "fields": {
                    "duration": 180
                }
            }
        ]
        # f'insert test_dev_ops, ip={dit.get("ip")} CPU={dit.get("cpu")} RAM={dit.get("memory")} DISK={dit.get("disk")} net_recvMB={dit.get("recvMB")} net_sentMB={dit.get("sentMB")}'
        ret = client.write_points(json_body)
        if not ret:
            logger.warning('主机:dit.get("ip") 数据录入 influxdb 异常')
        # print(ret)  # True

# 查询数据
# 前面已经插入了几条数据，下面我们演示查询。
# records = client.query("SELECT duration FROM brushEvents WHERE time > now() - 4d GROUP BY user")
# records = client.query("select * from test_dev_ops WHERE time > now() - 1d")
# for rec in records:
#     for i in rec:
#         print(i)
